package com.huan;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class ZookeeperTest {


    private String connectString = "huan01:2181,huan02:2181,huan03:2181";
    private int sessionTimeout = 2000;
    private ZooKeeper zkClit;

    //TODO 创建客户端
    @Before
    public void init() throws IOException {
        //TODO 传三个参数，第一个设置zk集群，第二个毫秒时间，第三个监听器
        zkClit =  new ZooKeeper( connectString,  sessionTimeout, new Watcher() {
            public void process(WatchedEvent event) {
                System.out.println("---start---");
                List<String> children ;
                try {
                    children = zkClit.getChildren( "/", true );
                    for (String child : children) {
                        System.out.println(child);
                    }
                    System.out.println("---end---");
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } );
    }

    //TODO 创建节点
    @Test
    public void createNode() throws KeeperException, InterruptedException {
        //TODO create 源码如下
        // public String create(final String path, byte data[], List<ACL> acl,CreateMode createMode)
        //        throws KeeperException, InterruptedException

        /**
         *  CREATE: 能创建子节点
         *  READ：能获取节点数据和列出其子节点
         *  WRITE: 能设置节点数据
         *  DELETE: 能删除子节点
         *  ADMIN: 能设置权限
         *
         *  public interface Ids {
         *
         *           This Id represents anyone.
         *
         *  public final Id ANYONE_ID_UNSAFE = new Id( "world", "anyone" );
         *
         *
         *           This Id is only usable to set ACLs. It will get substituted with the
         *           Id's the client authenticated with.
         *
         * public final Id AUTH_IDS = new Id( "auth", "" );
         *
         *           这是完全开放的ACL
         *           This is a completely open ACL .
         *
         *public final ArrayList<ACL> OPEN_ACL_UNSAFE = new ArrayList<ACL>(
                * Collections.singletonList( new ACL( Perms.ALL, ANYONE_ID_UNSAFE ) ));
         *
         *
         *           This ACL gives the creators authentication id's all permissions.
         *
         * public final ArrayList<ACL> CREATOR_ALL_ACL = new ArrayList<ACL>(
                * Collections.singletonList( new ACL( Perms.ALL, AUTH_IDS ) ));
         *
         *
         *          * This ACL gives the world the ability to read.
         *
         * public final ArrayList<ACL> READ_ACL_UNSAFE = new ArrayList<ACL>(
                * Collections
                *                         .singletonList( new ACL( Perms.READ, ANYONE_ID_UNSAFE ) ));
         *}
         */
        String path = zkClit.create( "/dahuanhuan", "huanhuan".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT );
        System.out.println(path);
    }
    //TODO 获取子节点并监听
    @Test
    public void getChildren() throws KeeperException, InterruptedException {
        List<String> children = zkClit.getChildren( "/", true );

        for (String child : children) {
            System.out.println(child);
        }
        //TODO 最大延迟
        Thread.sleep( Long.MAX_VALUE );
    }

    //TODO 判断Znode是否存在
    @Test
    public void exist() throws KeeperException, InterruptedException {
        Stat stat = zkClit.exists( "/huanma", false );

        System.out.println(stat == null? "no exists" : "exists");
    }
}
